/**
 * Created by mikezhangding on 21/2/2017.
 */
'use strict';

/*
 app.controller('accountCtrl', function ($state, $scope, $http, $localStorage,$ionicSlideBoxDelegate) {


 });*/


angular.module('account.module', [])
    .controller('coin_bankCtrl', function ($scope,$rootScope, $state, $http,$ionicLoading,$stateParams,ionicDatePicker,$filter,$ionicPopup,ionicTimePicker) {

        $scope.coin={};
        $scope.bankId = $stateParams.bankId;
        $scope.time = new Date(new Date().setDate(new Date().getDate()+3));
        var ipObj1 = {
      callback: function (val) {  //Mandatory
        console.log('Return value from the datepicker popup is : ' + val, new Date(val));
        $scope.time = new Date(val);
      }
    };
        $scope.coin.time='08:00';
        $scope.openDatePicker = function(){
      ionicDatePicker.openDatePicker(ipObj1);
    };
        var ipObj2 = {
            callback: function (val) {      //Mandatory
                if (typeof (val) === 'undefined') {
                    console.log('Time not selected');
                } else {
                    $scope.coin.time = epochParser(val);
                    console.log('选择的时间：' + epochParser(val));
                }
            }
        };
        function prependZero(param) {
            if (String(param).length < 2) {
                return "0" + String(param);
            }
            return param;
        }
        function epochParser(val) {
            if (val === null) {
                return "00:00";
            } else {
                var hours = parseInt(val / 3600);
                var minutes = (val / 60) % 60;
                return (prependZero(hours) + ":" + prependZero(minutes));
            }
        }
        $scope.openTimePicker =function () {
            ionicTimePicker.openTimePicker(ipObj2);
        };
        $scope.submit = function () {
        if($rootScope.bankNet.banknetId==null){
            var alertPopup = $ionicPopup.alert({
                title: '兑换提示!',
                template: '请选择银行网点！'
            });
            alertPopup.then(function(res) {
            });
            return false;
        }
        var order ='banknetId=' + $rootScope.bankNet.banknetId
            +'&remark=' + $rootScope.bankNet.netName
            +'&orderDate='+$filter('date')($scope.time,'yyyy-MM-dd') +' '+$scope.coin.time
            +'&openId='+$rootScope.openId;
        var num =0;
        if($scope.coin.wujiaonum!=null){
            order+='&wuJiaoNum='+$scope.coin.wujiaonum;
            num +=$scope.coin.wujiaonum;
        }
        if($scope.coin.yijiaonum!=null){
            order+='&yiJiaoNum='+$scope.coin.yijiaonum;
            num +=$scope.coin.yijiaonum;
        }
        if($scope.coin.yiyuannum!=null){
            order+='&yiYuanNum='+$scope.coin.yiyuannum;
            num +=$scope.coin.yiyuannum;
        }
        if(num<200){
            var alertPopup = $ionicPopup.alert({
                title: '兑换提示!',
                template: '兑换总数量不足200枚！'
            });
            alertPopup.then(function(res) {
            });
            return false;
        }
        if($scope.coin.time==null){
            var alertPopup = $ionicPopup.alert({
                title: '兑换提示!',
                template: '请填写时间！'
            });
            alertPopup.then(function(res) {
            });
            return false;
        }
        $http.get(API("huanbi/bankOrder?")+order).success(function (data) {
            var alertPopup = $ionicPopup.alert({
                title: '兑换提示!',
                template: '预约成功。1.请按预约时间到指定网点提取。逾期不取，超过2次，系统会取消用户注册!2.所有预约需求在各营业网点现场完成兑换手续及钱款交接，平台及营业网点不会以任何方式通知汇款、提前交款等方式进行预约兑换，也不会通知你在非营业场所办理兑换手续和钱款交接，请注意资金及人身安全。'
            });
            alertPopup.then(function(res) {
                $state.go('tabs.home');
            });
        });
    }
  })
    .controller('coin_bankNetCtrl', function ($scope,$rootScope, $state, $http,$ionicLoading,ionicDatePicker,$stateParams) {
        $scope.getBankNet = function () {
            $http.get(API("huanbi/bank/getBankNet?bankId=1&areaId=")+$stateParams.bankId).success(function (data) {
                 $scope.banks=data;
            })
        }
        $scope.getBankNet();
        $scope.bind_bankNet =function (bankNet) {
            $rootScope.bankNet = bankNet;
            $state.go("tabs.coin_bank");
        }
    })
    .controller('coinCtrl', function ($scope,$rootScope, $state, $http,$ionicPopup,openIdMessageService) {
        var message = openIdMessageService.getMessage();
        message.then(function success(result) {
            if(result.data.cardId==null||result.data.cardId==''){
                var alertPopup = $ionicPopup.alert({
                    title: '兑换提示!',
                    template: '请完善个人信息！'
                });
                alertPopup.then(function(res) {
                    $state.go('tabs.getUser');
                });
            }
        },function error(result) {
            var alertPopup = $ionicPopup.alert({
                title: '兑换提示!',
                template: '请完善个人信息！'
            });
            alertPopup.then(function(res) {
                $state.go('tabs.getUser');
            });
        });
})
    .controller('accountCtrl', function ($scope,$rootScope, $state, $http,$ionicLoading,ionicDatePicker) {

    })
    .controller('getUserCtrl', function ($scope,$rootScope, $state, $http,$ionicLoading,ionicDatePicker,$location,$ionicPopup) {
        $scope.user={};
        $location.replace();
        $scope.submit = function (isValid) {
            if(!isValid){
                return false;
            }
            var orderBy = "phone="+$scope.user.phone
                        +"&cardId="+$scope.user.cardId
                        +"&userName="+$scope.user.userName
                        +"&openId="+$rootScope.openId;
            $http.get(API("huanbi/bindUser?")+orderBy).success(function (result) {
                if(result.userId){
                    $rootScope.userMessage=true;
                    var alertPopup = $ionicPopup.alert({
                        title: '兑换提示!',
                        template: '提交成功！'
                    });
                    alertPopup.then(function(res) {
                        $state.go('tabs.account');
                    });
                    $state.go('tabs.account');
                }
            })
        }
    })
    .controller('viewUserCtrl', function ($scope,$rootScope, $state, $http,$ionicLoading,ionicDatePicker,$ionicPopup) {
        $scope.getUserDate  = function () {
            $http.get(API('huanbi/findUser?openId='+$rootScope.openId)).success(function (result) {
                if(result.successful){
                    $scope.user= result.data;
                }
            })
        }
        $scope.getUserDate();
        $scope.del  = function () {
            $http.get(API('/huanbi/unbindUser?openId='+$rootScope.openId)).success(function (result) {
                if(!result.successful){
                    $rootScope.userMessage=false;
                    var alertPopup = $ionicPopup.alert({
                        title: '兑换提示!',
                        template: '删除成功！'
                    });
                    alertPopup.then(function(res) {
                        history.back();
                    });
                }
            })
        }
        $scope.submit = function () {
            var confirmPopup = $ionicPopup.confirm({
                title: '删除提示',
                template: '<p style="color:red;text-align: center">您确定删除个人信息吗！</p>',
                cancelText:'取消',
                okText:'确定'
            });
            confirmPopup.then(function (res) {
                if (res) {
                    $scope.del();
                } else {
                    console.log('取消');
                }
            });
        }
    })
    .controller('historyCtrl', function ($scope,$rootScope, $state, $http,$ionicLoading,ionicDatePicker) {
        $scope.findMyOrder = function () {
            $http.get(API('huanbi/findMyOrder?openId='+$rootScope.openId)).success(function (result) {
                if(result){
                    $scope.orderList = result.slice().reverse();
                    $scope.orderList.sort(function(a,b){
                        return b.orderDate-a.orderDate});
                }
            })
        }
        $scope.findMyOrder();
    })
    .controller('complainCtrl', function ($scope,$rootScope, $state, $http,$ionicPopup) {
        // if(!$rootScope.userMessage){
        //     $state.go('tabs.account')
        // }
        $scope.suggestion={};
        $http.get(API('huanbi/findUser?openId='+$rootScope.openId)).success(function (result) {
            $scope.suggestion.contact = result.data.phone;
            }
        )
        $scope.submit = function (isValid) {
            if(!isValid){
                return false;
            }
            var complain = "title="+$scope.suggestion.title
                +"&content="+$scope.suggestion.content
                +"&contact="+$scope.suggestion.contact
                +"&openid="+$rootScope.openId;
            $http.get(API("huanbi/suggestion/addSuggestion?")+complain).success(function (result) {
                if(result.successful){
                    var alertPopup = $ionicPopup.alert({
                        title: '兑换提示!',
                        template: '提交成功！'
                    });
                    alertPopup.then(function(res) {
                        $state.go('tabs.account');
                    });
                }
            })
        }


    })
    .controller('coin_listCtrl', function ($scope,$rootScope, $state, $http,$ionicLoading,ionicDatePicker,$stateParams) {
        $scope.getBankNet = function () {
            $http.get(API('huanbi/coin/queryChanger')).success(function (result) {
                $scope.banks=result.data;
            })
        }
        $scope.getBankNet();
    })
